package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_user_admin;
import cn.com.api.entity.ApiT_user_role;

import cn.com.api.service.ApiT_user_actionService;
import cn.com.api.service.ApiT_user_adminService;
import cn.com.api.service.ApiT_user_roleService;
import cn.com.api.service.ApiT_user_role_actionService;
import cn.com.api.service.StartOnLoadService;
import cn.com.backstage.controller.base.T_user_adminBaseController;

import cn.com.util.Tools;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_user_adminController  extends T_user_adminBaseController {

	private static Logger logger = Logger.getLogger(T_user_adminController.class);
	
	@Autowired
	private ApiT_user_adminService apiT_user_adminService;
	@Autowired
	private ApiT_user_roleService apiT_user_roleService;
	@Autowired
	private ApiT_user_actionService apiT_user_actionService;
	@Autowired
	private ApiT_user_role_actionService apiT_user_role_actionService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	/**
	 * 个人中心页面
	 * @return
	 */
	@RequestMapping(value = "t_user_admin-center")
	public ModelAndView t_user_admin_center(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_admin_center.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_user_admin t_user_admin = apiT_user_adminService.findT_user_adminById(Integer.valueOf(id));
			mv.addObject("t_user_admin",t_user_admin);
		}
		
		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
		mv.addObject("t_user_admin",admin);
		
		return mv;
	}
	
	/**
	 * 管理员列表
	 * @return
	 */
	@RequestMapping(value = "user_admin-list")
	public ModelAndView user_admin_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_admin_list.jsp");
		
		List<ApiT_user_admin> t_user_admins = null;
		if(request.getParameter("role_id")!=null){
			String role_id = request.getParameter("role_id");
			t_user_admins = apiT_user_adminService.findT_user_adminsByRole_id(role_id);
		}else{
			t_user_admins = apiT_user_adminService.findT_user_admins();
		}
		mv.addObject("t_user_admins", t_user_admins);
		
		List<ApiT_user_role>  t_user_roles = apiT_user_roleService.findT_user_roles();
		mv.addObject("roles", t_user_roles);
		
		List<ApiT_city> cities = startOnLoadService.citys();
		mv.addObject("citys", cities);
		
		return mv;
	}

	/**
	 *编辑
	 * @return
	 */
	@RequestMapping(value = "t_user_admin-center-edit")
	public void t_user_admin_center_edit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String true_name = request.getParameter("true_name");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String new_password = request.getParameter("new_password");
		String city_id = request.getParameter("city_id");
		String role_id = request.getParameter("role_id");
		String status = request.getParameter("status");
		String is_super = request.getParameter("is_super");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){

		}
		//edit
		else{
			ApiT_user_admin t_user_admin = apiT_user_adminService.findT_user_adminById(Integer.valueOf(id));
			String old_password = Tools.toMD5(Tools.RmFilter(password));
			if(old_password.equalsIgnoreCase(t_user_admin.getPassword())){
			    password = Tools.toMD5(Tools.RmFilter(new_password));
			    t_user_admin.setPassword(password);
		
				apiT_user_adminService.updateT_user_admin(t_user_admin);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "修改失败,请输入正确的旧密码！");
			}
		}
		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



